#coding:utf-8
import scrapy
import json
import unicodedata
import re
import computeDaysByNums
import time
import MySQLdb

class QuotesSpider(scrapy.Spider):
    name = "contributor_nums"

    def start_requests(self):
        tag = getattr(self, 'tag', None)
        tid = getattr(self, 'tid', None)
        if tag is not None:
            # yield scrapy.Request(url, meta={'dont_redirect':True}, headers=header,callback=self.parse)
            yield scrapy.Request(tag, callback=self.parse)

    def get_contributors_num(self, response):
        json_to_python = json.loads(response.body.decode(response.encoding))
        data = json_to_python['series'][0]['data']
        #如果不够三年的数据，有多少算多少
        #如果超出三年，有多少算多少
        if len(data) > 36:
            data = data[-36:]
        #print data
        #删掉季度之外多余的月份
        # length = len(data) / 3 * 3
        # if len(data) % 3 is not 0:
        #     data = data[-length]
        # res = 0
        # con_per_quar = {}
        con_per_mon = {}
        for item in data:
            data_str = str(item[0])[0:-3] + '.000'
            #时间戳转换为时间元组，再格式化输出
            data_num = time.strftime("%Y-%m-%d", time.localtime(float(data_str)))
            con_per_mon[data_num] = item[1]
        yield con_per_mon

    def parse(self, response):
        code_url = response.css("div[class='chart watermark440']::attr(datasrc)").extract()
        yield scrapy.Request(code_url[2], self.get_contributors_num)

    def insert_into_commu(con_per_mon):
        db = MySQLdb.connect("localhost","root","root","unispider")
        cursor = db.cursor()
        cursor.executemany("insert tbl_oss_sp_commu_src(tid, d, key_val, val_from) values(%s,%s,%s,%s)", (tid, key_name, key_val, val_from))

        db.commit()

        data = cursor.fetchone()

        print "reply:",data

        db.close()
